package com.china08.yxyapi.api.manage.sch.platform.website.image;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.mg.sws.SwsAlbums;
import com.china08.yxyapi.entity.mg.sws.SwsAlbumsImage;
import com.china08.yxyapi.model.YxyApiPageReqModel;
import com.china08.yxyapi.repository.mg.SwsAlbumsImageRepository;
import com.china08.yxyapi.repository.mg.YxySwsAlbumsRepository;

import io.swagger.annotations.ApiOperation;

@CrossOrigin
@RestController
public class SwsAlbumsAddImgController extends YxyApiBaseController {

    @Autowired
    SwsAlbumsImageRepository swsAlbumsImageRepository;

    @Autowired
    YxySwsAlbumsRepository yxySwsAlbumsRepository;

    @ApiOperation("追加相册图片")
    @RequestMapping(value = "/api_yw/manage/sch/platform/website/swsalbums/add/image", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public void precess(HttpServletRequest request, HttpServletResponse response, @RequestBody ReqModel reqModel) {

        String id = reqModel.id;
        List<String> imgList = reqModel.imgList;
        String schoolId = getProfile(request, response).getSchoolId();

        // 相册
        SwsAlbums swsAlbums = yxySwsAlbumsRepository.findById(id);

        // 获取排序最大值
        SwsAlbumsImage swsAlbumsImage = swsAlbumsImageRepository.findTop1ByAlbumIdOrderByOrderNumDesc(id);
        Integer orderNum = (swsAlbumsImage == null) ? 1 : swsAlbumsImage.getOrderNum() + 1;

        // 没有封面,设置相册图片总数量
        if (swsAlbums != null && StringUtils.isBlank(swsAlbums.getCovers())) {
            swsAlbums.setCovers(imgList.get(0));
            Integer imageCount = swsAlbums.getImageCount() + imgList.size();
            swsAlbums.setImageCount(imageCount);
            yxySwsAlbumsRepository.save(swsAlbums);
        }

        for (String img : imgList) {
            swsAlbumsImage = new SwsAlbumsImage();
            swsAlbumsImage.setAlbumId(id);
            swsAlbumsImage.setSchoolId(schoolId);
            swsAlbumsImage.setOrderNum(orderNum);
            swsAlbumsImage.setImagePath(img);
            orderNum = orderNum + 1;
            swsAlbumsImageRepository.save(swsAlbumsImage);
        }

        // 获取相册图片总数量
        Integer countInteger = swsAlbumsImageRepository.countByAlbumId(id);
        swsAlbums.setImageCount(countInteger);
        yxySwsAlbumsRepository.save(swsAlbums);
    }

    protected static class ReqModel extends YxyApiPageReqModel {
        public String id;
        public List<String> imgList;

        @Override
        public void autoCheck() {
            Assert.isTrue(StringUtils.isNotBlank(id), "添加相册不能为空");
            Assert.isTrue(imgList.size() > 0, "添加图片不能为空");
        }
    }
}
